package dm.ast.model.gen;

import com.sun.org.apache.xpath.internal.operations.Bool;
import dm.ast.DmASTNode;
import dm.ast.DmBoolExpression;
import dm.ast.DmBoolOperator;
import dm.ast.DmWhere;
import dm.ast.model.gen.ASTGenBase;
import dm.ast.model.gen.factory.ComplexityModel;
import dm.ast.model.gen.factory.WheModelFactory;

/**
 * 根据模板AST生成的WHERE子句
 */
public class WhereGen extends ASTGenBase {

    @Override
    public DmASTNode gen(int astLevel, int wid_t, int dep_t) {
        int outerjoinwid = 3;
        ComplexityModel model = WheModelFactory.whereModels(wid_t-3, dep_t);
        //此处的testFlags表示查询谓词的种类
        BoolExpressionGen boolGen = new BoolExpressionGen(testFlags);
        DmBoolExpression boolExp1 = boolGen.singleGen(astLevel , model.comps[0].wid_t , model.comps[0].dep_t);
        DmWhere where = new DmWhere(boolExp1);

        for (int i = 1; i < model.count; i++) {
            DmBoolExpression boolExpTmp = boolGen.singleGen(astLevel , model.comps[i].wid_t , model.comps[i].dep_t);

            if (boolExpTmp== null) {
                System.out.println("为空");
            }
            //产生一个随机的2元bool操作符
            DmBoolOperator op;
            do{
                op = OperatorGen.boolOperatorRandom();
                if(op.getExpNum() != 1){
                    break;
                }

            }while(true);
            where.addWhereCondition(boolExpTmp , op);
        }
        where.outerjoinwid = outerjoinwid;
        return where;
    }

    //带（+）类型
    public static void singlejoin(int astLevel,DmWhere where){
        BoolExpressionGen boolGen=new BoolExpressionGen();
        DmBoolExpression boolExp2 = boolGen.SingJoinGen(astLevel,where.outerjoinwid,1);
        DmBoolOperator op1;
        do {//没有保证与OR连接
            op1 = OperatorGen.boolOperatorRandom();
        }while (op1.getOperate()=="OR");
        where.addWhereCondition(boolExp2,op1);
    }
}
